package com.swpu.library.sys.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.swpu.library.sys.entity.Roles;
import com.swpu.library.sys.service.IRolesService;
import com.swpu.library.sys.utils.ResponseUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hui
 * @since 2020-11-23
 */
@RestController
@CrossOrigin
@RequestMapping("/sys/roles")
public class RolesController {
   @Autowired
    IRolesService rolesService;
   @RequestMapping(method = RequestMethod.GET)
   public Object index(){
       List<Roles> list = rolesService.list();
       return ResponseUtils.ok(list);
   }
   @RequestMapping(method = RequestMethod.POST)
   public Object insertOne(@RequestBody Roles roles){
       rolesService.save(roles);
       return ResponseUtils.ok();
   }
   @RequestMapping(method = RequestMethod.DELETE)
    public Object deletedById(@Param("id") Integer id) {
       rolesService.removeById(id);
       return ResponseUtils.ok();
   }
   @RequestMapping(method = RequestMethod.PUT)
   public Object update(@RequestBody Roles roles){
       if(roles.getId()==null) {
           ResponseUtils.badRequest();
       }
       UpdateWrapper<Roles> wrapper =new UpdateWrapper<>();
       wrapper.lambda().eq(Roles::getId,roles.getId());
       rolesService.update(roles,wrapper);
       return ResponseUtils.ok();
   }
}
